package com.hqyj.zss.controller;

import com.hqyj.zss.pojo.Employee;
import com.hqyj.zss.pojo.Order;
import com.hqyj.zss.pojo.PageData;
import com.hqyj.zss.service.EmployeeOrderService;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/employeeOrder")
public class EmployeeOrderController {

    @Autowired
    private EmployeeOrderService employeeOrderService;

    //查询所有订单信息
    @RequestMapping("/queryAllOrder")
    public String queryAllOrder(ModelMap modelMap, Integer pageNumber, Integer pageSize,HttpServletRequest req){
        if(pageNumber == null){
            pageNumber = 1;
        }
        if(pageSize == null){
            pageSize = 5;
        }
        PageData pageData = employeeOrderService.queryAllOrder(pageNumber,pageSize);
        modelMap.put("pageData", pageData);
        int orderTotal = employeeOrderService.orderTotal();
        req.setAttribute("orderTotal",String.valueOf(orderTotal));
        int allMoney = employeeOrderService.getAllMoney();
        req.setAttribute("allMoney",String.valueOf(allMoney));
        int getMoney=allMoney-orderTotal;
        req.setAttribute("getMoney",String.valueOf(getMoney));
        return "employeeOrderList";
    }

    //取消订单
    @RequestMapping("/deleteOrder")
    @ResponseBody
    public String deleteOrder(HttpServletRequest request){
        String o_id = request.getParameter("o_id");
        int i = employeeOrderService.deleteOrder(Integer.parseInt(o_id));
        if(i>0){
            return "success";
        }
        return "fail";
    }

    //返回updateOrder页面
    @RequestMapping("/update")
    public String updateEmployee(HttpServletRequest request){
        String o_id = request.getParameter("o_id");
        int i = Integer.parseInt(o_id);
        //根据id查询订单信息
        Order order = employeeOrderService.queryOneOrder(i);
        request.setAttribute("Order",order);
        return "updateOrder";
    }

    //修改订单状态
    @RequestMapping("updateOrder")
    @ResponseBody
    public String updateOrder(HttpServletRequest request){
        String o_id = request.getParameter("o_id");
        String o_state = request.getParameter("o_state");
        int i = employeeOrderService.updateOrder(Integer.parseInt(o_id),Integer.parseInt(o_state));
        if(i>0){
            return "success";
        }
        return "fail";
    }
}
